﻿using BussinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using TestingSystem.Common;

namespace TestingSystem.TestingManager
{
    public partial class TestingManagerForm : Form
    {
        private BindingList<UserTestCourse> listUserTestCourse = new BindingList<UserTestCourse>();

        public TestingManagerForm()
        {
            InitializeComponent();
        }

        private void TestingManagerForm_Load(object sender, EventArgs e)
        {
            try
            {
                // Load and binding list courses to course combobox
                this.cboCourse.DataSource = this.GetCourses();
                this.cboCourse.SelectedIndex = -1;

                // Load and binding list users to user combobox
                this.cboLecture.DataSource = this.GetUsers();
                this.cboLecture.SelectedIndex = -1;

                this.dgvTest.DataSource = listUserTestCourse;
            }
            catch (Exception ex)
            {
                // Show error message
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                // Load data and show in datagrid view
                var filter = new Filter(null, null, null);
                if (this.cboCourse.SelectedIndex >= 0)
                {
                    filter.Course_ID = this.cboCourse.SelectedValue.ToString();
                }
                if (this.cboLecture.SelectedIndex >= 0)
                {
                    filter.User_ID = this.cboLecture.SelectedValue.ToString();
                }
                if (this.chkEnableStarttime.Checked)
                {
                    filter.Starttime = this.dtpStarttime.Value;
                }
                var list = BLL_Test.Load(new TEST
                {
                    COURSE_ID = filter.Course_ID,
                    USER_ID = filter.User_ID,
                    TEST_START_TIME = filter.Starttime.HasValue == true ? filter.Starttime.Value : DateTime.MinValue
                });

                this.listUserTestCourse.Clear();
                foreach (var item in list)
                {
                    this.listUserTestCourse.Add(new UserTestCourse
                                           {
                                               Test_ID = item.TEST_ID,
                                               Course_ID = item.COURSE_ID,
                                               Course_Name = item.COURSE_NAME,
                                               User_ID = item.USER_ID,
                                               Full_Name = item.LAST_NAME + ", " + item.FIRST_NAME,
                                               Test_Creation_Date = item.TEST_CREATION_DATE,
                                               Test_Desc = item.TEST_DESC,
                                               Test_Starttime = item.TEST_STARTTIME,
                                               Test_Duration = item.TEST_DURATION
                                           });
                }
            }
            catch (Exception ex)
            {
                // Show error message
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            // Open testing creation form
            TestingAddEditForm addEditTestingForm = new TestingAddEditForm("");
            if (addEditTestingForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.btnLoad_Click(sender, e);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // Check user select a row
            if (this.dgvTest.SelectedRows.Count <= 0)
            {
                MessageBox.Show("Please select a testing to edit.", "Edit", MessageBoxButtons.OK);
            }
            else
            {
                // Show a form edit the testing
                var test = this.dgvTest.SelectedRows[0];
                TestingAddEditForm addEditTestingForm = new TestingAddEditForm(test.Cells[0].Value.ToString());
                if (addEditTestingForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.btnLoad_Click(sender, e);
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // Check user select a row
            if (this.dgvTest.SelectedRows.Count <= 0)
            {
                MessageBox.Show("Please select a testing to delete.", "Delete", MessageBoxButtons.OK);
            }
            else
            {
                // Delete a row
                try
                {
                    if (MessageBox.Show("Do you want delete?", "Delete", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        var test = this.dgvTest.SelectedRows[0];
                        bool result = BLL_Test.Delete(new TEST
                        {
                            TEST_ID = test.Cells[0].Value.ToString()
                        });

                        if (result)
                        {
                            MessageBox.Show("The testing was deleted.", "Delete", MessageBoxButtons.OK);

                            this.listUserTestCourse.Remove(test.DataBoundItem as UserTestCourse);
                        }
                        else
                        {
                            MessageBox.Show("The error occured when delete testing. Please try again.", "Error", MessageBoxButtons.OK);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // Show error message
                    MessageBox.Show("The error occured when delete testing. Please try again.", "Error", MessageBoxButtons.OK);
                }
            }
        }

        private void dgvTest_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvTest.SelectedRows.Count > 0)
            {
                this.btnEdit.Enabled = true;
                this.btnDelete.Enabled = true;
            }
            else
            {
                this.btnEdit.Enabled = false;
                this.btnDelete.Enabled = false;
            }
        }

        #region Helper Methods

        private List<Course> GetCourses()
        {
            var listCourse = new List<Course>();

            try
            {
                var listDTOCourse = BLL_Course.LoadAll();
                foreach (var item in listDTOCourse)
                {
                    listCourse.Add(new Course(item.COURSE_ID, item.COURSE_NAME));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listCourse;
        }

        private List<User> GetUsers()
        {
            var listUser = new List<User>();

            try
            {
                var listDTOUser = BLL_User.LoadAll();
                foreach (var item in listDTOUser)
                {
                    listUser.Add(new User(item.USER_ID, item.USER_LAST_NAME + ", " + item.USER_FIRST_NAME));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listUser;
        }

        #endregion
    }
}
